/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.task;

import com.cxhd.dc.DataConfig;
import com.cxhd.dc.tool.CxhdTran;
import com.cxhd.dc.tool.FileFilterImpl;
import com.cxhd.dc.tool.FileUtil;
import it.sauronsoftware.ftp4j.FTPClient;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class UploadFileTask extends BaseTask {

    private List<File> list = new ArrayList<File>();
    private FTPClient ftpClient;
    private int uploadFlag = 0;
    private boolean close = true;
    private String file_path;
    private static final Logger logger1 = Logger.getLogger("UploadFile");

    @Override
    public BaseTask start() {
        //logger1.info("start-----------------22222222222222222222");
        logger1.info(DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getZipFolder() + ":path");
        file_path = DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getZipFolder();
        // file_path="K:/dataBase/zipFolder";
        list = Arrays.asList(new File(file_path).listFiles(new FileFilterImpl(".u"))); //FileUtil.getFile(file_path, true, ".u");
        logger1.info("获取到file");
        //   System.out.println(list.size() + "-------------");
//        Collections.sort(list, new Comparator<File>() {
//
//            @Override
//            public int compare(File o1, File o2) {
//                if (o1.lastModified() > o2.lastModified()) {
//                    return 1;
//                }
//                return -1;
//            }
//        });
        if (list != null && !list.isEmpty()) {
            logger1.info(list.size());
            try {
                ftpClient = CxhdTran.getInstance().getClient();
            } catch (Exception ex) {
                logger1.info(ex);
            }
        }
        return this;
    }

    @Override
    public BaseTask doTask() {

        if (ftpClient != null && ftpClient.isConnected()) {
            for (File f : list) {
                try {
                    if (f.length() > 0) {

                        uploadFlag = CxhdTran.getInstance().upload(ftpClient, f.getName().split("_")[0].toUpperCase() + "/inbox", true, f);
                        System.out.println("*****************uploadFlag:" + uploadFlag);
                        if (uploadFlag == 1) {
                            ftpClient.logout();
                            break;

                        }
                    } else {
                        f.delete();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger1.info(e);
                }
            }
        } else {
            logger1.info("连接不上服务器");
        }

        return this;
    }

    public void reuploadFile() {
        if (close) {
            start().doTask().stop();
        }
    }

    @Override
    public BaseTask stop() {
        if (ftpClient != null) {
            CxhdTran.getInstance().logout(ftpClient);
        }
//        if (uploadFlag == 1) {
//            logger1.info("重新执行任务----------");
//            start().doTask().stop();
//        }
//        list = null;
//        list = FileUtil.getFile(file_path, true, ".u");
//        if (list != null && !list.isEmpty()) {
//            start().doTask().stop();
//        }
        return this;
    }

    @Override
    public BaseTask cancelTask() {
        close = false;
        if (ftpClient != null) {
            CxhdTran.getInstance().logout(ftpClient);
        }
        return this;
    }

}
